package wk.demo.activities;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.luoye.bzijkplayer.R;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import wk.demo.tool.MyToastTool;

public abstract class BashCompatActivity extends AppCompatActivity {
    public Context mcontext ;
    private TextView tilteTv ;
    public CompositeDisposable compositeDisposable ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mcontext =  this ;
        requestPermission(this);

        compositeDisposable = new CompositeDisposable() ;
        initView() ;

    }



    private void initView(){
        findViewById(R.id.goback).setOnClickListener(onClickListener);
        findViewById(R.id.action_recent).setOnClickListener(onClickListener);
        findViewById(R.id.action_remmoveUrl).setOnClickListener(onClickListener);
        findViewById(R.id.action_settings).setOnClickListener(onClickListener);
        tilteTv = findViewById(R.id.tilteTv) ;
    }


    public void setTtitleMsg( String msg){
        tilteTv.setText(msg);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id =  v.getId();
            if (id == R.id.action_settings) {
                SettingsActivity.intentTo(mcontext);
            } else if (id == R.id.action_recent) {
                RecentMediaActivity.intentTo(mcontext);
            } else if (id == R.id.action_remmoveUrl) {
                RemoteUrlMediaActivity.intentTo(mcontext);
            }else if(id==R.id.goback){
                Activity activity = ((Activity)mcontext);
                if(activity instanceof  FileExplorerActivity){
                    onKeyBackClick();
                }else{
                    activity.finish();
                }
            }
        }
    };

    private void requestPermission(Context context) {
        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                .permission( Manifest.permission.READ_EXTERNAL_STORAGE )
                .permission(  Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        //
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if (quick) {
                            MyToastTool.toastMsg(context , "拒绝授权将导致APP异常");
                            XXPermissions.startPermissionActivity(context, denied);
                        }
                    }
                });
    }


    public void showToastMsg(String msg){
        Toast.makeText(mcontext , msg , Toast.LENGTH_SHORT).show();
    }

    private long lastTime = 0 ;
    public void onKeyBackClick(){
        long distance = System.currentTimeMillis() -lastTime ;
        if( distance <  2000){
            finish();
        }else{
            showToastMsg("再按一次退出");
            lastTime = System.currentTimeMillis();
        }
    }


    public void addToCompositeDisposable(Disposable disposable){
        if(compositeDisposable!=null){
            compositeDisposable.add(disposable) ;
        }
    }






    @Override
    public void finish() {
        super.finish();
        if (compositeDisposable!=null){
            if(!compositeDisposable.isDisposed()){
                compositeDisposable.dispose();
                compositeDisposable.clear();
            }
        }
    }
}
